import akshare as ak
import matplotlib.pyplot as plt

stock=input('请输入股票代码：')
# 获取stock的股票数据，日期范围从 2020-01-01 到 2021-01-01
stock_data = ak.stock_zh_a_hist(stock,period= "daily", start_date="20240101" , end_date='20241218')

# 计算 50 日和 200 日的移动平均线
stock_data['SMA_50'] = stock_data['收盘'].rolling(window=50).mean()
stock_data['SMA_200'] = stock_data['收盘'].rolling(window=200).mean()


# 绘制收盘价和移动平均线
plt.figure(figsize=(12, 6))
plt.plot(stock_data['日期'], stock_data['收盘'], label='Close Price')
plt.plot(stock_data['日期'], stock_data['SMA_50'], label='50-Day SMA')
plt.plot(stock_data['日期'], stock_data['SMA_200'], label='200-Day SMA')
plt.title('Maotai Stock Price with Moving Averages', fontsize=14)
plt.xlabel('Date', fontsize=12)
plt.ylabel('Price (CNY)', fontsize=12)
plt.legend()
plt.grid(True)
plt.show()